import { Component, OnInit } from '@angular/core';
import { NavController, NavParams, ToastController } from 'ionic-angular';
import { Park } from '../../providers/park-data/park';
import { User } from '../../providers/login-data/user';
import { Storage } from '@ionic/storage';
import { CommentInfo } from '../../providers/comment-data/comment';
import { CommentDataProvider } from '../../providers/comment-data/comment-data';

/**
 * Generated class for the ParkCommentPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@Component({
  selector: 'page-park-comment',
  templateUrl: 'park-comment.html',
})
export class ParkCommentPage implements OnInit {

  park:Park;
  user:User;
  commentContent:string;

  constructor(public navCtrl: NavController, public navParams: NavParams,
    public storage:Storage,
    public commentDataProvider:CommentDataProvider,
    public totastCtrl:ToastController,) {
  }

  ngOnInit():void{
    this.park=this.navParams.get('park');
  }
  ionViewDidLoad() {
    this.storage.get('LOGIN_INFO').then(val=>{
      this.user=JSON.parse(val);
    });
  }

  postComment(){
    if(this.commentContent.length>0){
      let _comment:CommentInfo={
        content:this.commentContent,
        userid:this.user.objectId,
        parkid:this.park.objectId
      };

      this.commentDataProvider.addComment(_comment).toPromise().then(()=>{
        this.navCtrl.pop();
      }).then(()=>{
        this.showToast('botom','评论成功');
      });
    }
  }

  showToast(position:string,message:string){
    let toast=this.totastCtrl.create({
      message:message,
      duration:2000,
      position:position
    });
    toast.present();
  }
}
